<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Profile</title>

    <link rel="stylesheet" href="style.css">

        <style>

        body {

            font-family: 'Arial', sans-serif;
            background-image: radial-gradient(circle at 50% 50%, rgba(0, 255, 136, 0.1) 0%, transparent 50%), linear-gradient(to bottom, #0a0b1a 0%, #1a1b3a 100%);
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;

        }

        .container {

            background-color: #0b0e1c;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 255, 136, 0.2);
            border-color: #00d0ff;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            width: 90%;
            max-width: 800px;
            margin: auto;

        }

        .edit-profile {

            text-align: center;
            margin-bottom: 20px;
            width: 100%;

        }

        .edit-profile h3 {

            margin-bottom: 10px;
            color: #ffffff;
            font-size: 24px;

        }

        .home-link {

            display: flex;
            align-items: center;
            color: #00d0ff;
            font-size: 18px;
            text-decoration: none;
            position: absolute;
            margin-left: 20px;

        }

        .home-link i {

            margin-right: 8px;
            font-size: 24px;

        }

        .home-link:hover {

            color: #ffffff;

        }

        .avatar-selection {

            text-align: center;
            margin-bottom: 20px;

        }

        .avatars {

            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 20px;

        }

        .avatars img {

            width: 120px;
            height: 120px;
            cursor: pointer;
            border: 4px solid transparent;
            border-radius: 50%;
            transition: transform 0.3s ease-in-out, border-color 0.3s ease-in-out;

        }

        .avatars img:hover {

            transform: scale(1.1);

        }

        .avatars img.selected {

            border-color: #00d0ff;

        }

        .profile-details {

            text-align: center;
            margin-bottom: 20px;
            width: 100%;

        }

        .profile-details h4 {

            margin-bottom: 10px;
            color: #ffffff;
            font-size: 20px;

        }

        .profile-details input {

            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
            background: transparent;
            border-color: #00d0ff;
            color: white;

        }

        .save-button {

            background-color: #00d0ff;
            color: #fff;
            border: none;
            padding: 10px 20px;
            font-size: 18px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease-in-out, transform 0.2s ease-in-out;
            margin-top: 20px;

        }

        .save-button:hover {

            background-color: #0087a5;
            transform: scale(1.05);

        }

        .save-button:focus {

            outline: none;

        }

        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {

            margin: 0;
            padding: 0;
            box-sizing: border-box;

        }

        .container {

            max-width: 1170px;
            margin: auto;
            max-height: 650px;

        }

        .row {

            display: flex;
            flex-wrap: wrap;

        }

        ul {

            list-style: none;

        }

    </style>

</head>

<body>

    <div class="container">
        <div class="edit-profile">
            <!-- Home Link -->
            <a href="../../index.html" class="home-link">
                <i class='bx bx-home'></i> Home
            </a>

            <h3>Edit Profile</h3>
        </div>
        <div class="avatar-selection">
            <h4>Select Avatar</h4>
            <div class="avatars">
                <img src="https://i.postimg.cc/VvvM4LLK/4140061.png" onclick="selectAvatar('https://i.postimg.cc/VvvM4LLK/4140061.png')" />
                <img src="https://i.postimg.cc/g0yv2TTH/6833605.png" onclick="selectAvatar('https://i.postimg.cc/g0yv2TTH/6833605.png')" />
                <img src="https://i.postimg.cc/NFFmtc9K/xnz-Mst5-V-400x400.jpg" onclick="selectAvatar('https://i.postimg.cc/NFFmtc9K/xnz-Mst5-V-400x400.jpg')" />
                <img src="https://i.postimg.cc/rwKwFnfR/4139967.png" onclick="selectAvatar('https://i.postimg.cc/rwKwFnfR/4139967.png')" />
                <img src="https://i.postimg.cc/dQNG3d4p/6997484.png" onclick="selectAvatar('https://i.postimg.cc/dQNG3d4p/6997484.png')" />
            </div>
        </div>
        <div class="profile-details">
            <h4>Edit Details</h4>
            <input type="text" id="nameInput" placeholder="Enter Name" required required pattern="[a-zA-Z ]+" oninvalid="this.setCustomValidity('Numbers and Symbols are not allowed')" oninput="this.setCustomValidity('')">
            <input type="email" id="emailInput" placeholder="Enter Email" required>
            <input type="tel" id="phoneInput" placeholder="Enter Phone" required>
            <input type="text" id="bioInput" placeholder="Add Bio" maxlength="150" required>
        </div>
        <button class="save-button" onclick="saveProfile()">Save Profile</button>
    </div>

    <script>

        let selectedAvatar = "";

        function selectAvatar(avatarSrc) {
            const avatars = document.querySelectorAll('.avatars img');
            avatars.forEach(img => {
                img.classList.remove('selected');
            });
            const selectedImg = Array.from(avatars).find(img => img.src === avatarSrc);
            if (selectedImg) {
                selectedImg.classList.add('selected');
                selectedAvatar = avatarSrc;
            }
        }

        function saveProfile() {
            const newName = document.getElementById("nameInput").value.trim();
            const newEmail = document.getElementById("emailInput").value.trim();
            const newPhone = document.getElementById("phoneInput").value.trim();
            const newBio = document.getElementById("bioInput").value.trim();

            // Check if all fields are valid before proceeding
            if (!nameInput.checkValidity() || !emailInput.checkValidity() || !phoneInput.checkValidity()) {
                nameInput.reportValidity();
                emailInput.reportValidity();
                phoneInput.reportValidity();
                bioInput.reportValidity();
                return;  // Stop function execution if invalid
            }

            if (newEmail && !validateEmail(newEmail)) {
                alert("Invalid email address");
                return;
            }

            if (selectedAvatar) {
                localStorage.setItem("selectedAvatar", selectedAvatar);
            }
            if (newName) {
                localStorage.setItem("profileName", newName);
            }
            if (newEmail) {
                localStorage.setItem("profileEmail", newEmail);
            }
            if (newPhone) {
                localStorage.setItem("profilePhone", newPhone);
            }
            if (newBio) {
                localStorage.setItem("profileBio", newBio);
            }

            // Redirect to the profile page
            window.location.href = "profile.html";  // Change made here
        }

        function validateEmail(email) {
            const re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
            return re.test(email);
        }

        document.addEventListener("DOMContentLoaded", () => {
            const savedAvatar = localStorage.getItem("selectedAvatar");
            if (savedAvatar) {
                const selectedImg = Array.from(document.querySelectorAll('.avatars img')).find(img => img.src === savedAvatar);
                if (selectedImg) {
                    selectedImg.classList.add('selected');
                    selectedAvatar = savedAvatar;
                }
            }

            const savedName = localStorage.getItem("profileName");
            const savedEmail = localStorage.getItem("profileEmail");
            const savedPhone = localStorage.getItem("profilePhone");
            const savedBio = localStorage.getItem("profileBio");

            if (savedName) {
                document.getElementById("nameInput").value = savedName;
            }
            if (savedEmail) {
                document.getElementById("emailInput").value = savedEmail;
            }
            if (savedPhone) {
                document.getElementById("phoneInput").value = savedPhone;
            }
            if (savedBio) {
                document.getElementById("bioInput").value = savedBio;
            }
        });

    </script>

</body>

</html>
